package com.supereye.platform.web.expert.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.supereye.platform.common.ResultMsg;
import com.supereye.platform.util.PropUtil;
import com.supereye.platform.web.expert.model.TopExpert;
import com.supereye.platform.web.expert.service.ExpertService;

@Controller
@RequestMapping("/expert")
public class ExpertController {
	@Resource 
	private ExpertService expertService;
	
	@RequestMapping("/addExpert")
	@ResponseBody
	public ResultMsg addExpert(HttpServletRequest request,HttpServletResponse response,TopExpert expert) throws Exception{
		try{
			expertService.addExpert(expert);
			return new ResultMsg(ResultMsg.SUCCESS,"新增成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,e.getMessage());
		}
	}
	
	@RequestMapping("/updateExpert")
	@ResponseBody
	public ResultMsg updateExpert(HttpServletRequest request,HttpServletResponse response,TopExpert expert) throws Exception{
		try{
			expertService.updateExpert(expert);
			return new ResultMsg(ResultMsg.SUCCESS,"修改成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"修改失败");
		}
	}
	
	@RequestMapping("/getExpertList")
	@ResponseBody
	public String getExpertList(HttpServletRequest request,HttpServletResponse response,Integer pageNum,Integer pageSize,TopExpert expert) throws Exception{
		String result = expertService.getExpertList(pageNum,pageSize,expert);
		return result;
	}
	
	@RequestMapping("/deleteExperts")
	@ResponseBody
	public ResultMsg deleteUsers(HttpServletRequest request,HttpServletResponse response,String expertIds) throws Exception{
		try{
			expertService.deleteExperts(expertIds.split(","));
			return new ResultMsg(ResultMsg.SUCCESS,"删除成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,"删除失败");
		}
	}
	
	@RequestMapping("/exportExperts")
	@ResponseBody
	public ResultMsg exportUsers(@RequestParam("file") MultipartFile file) throws Exception{
		try{
			String result = expertService.exportExperts(file);
	        return new ResultMsg(ResultMsg.SUCCESS,"导入成功",result);
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,e.getMessage());
		}
	}
	
	@RequestMapping("/confirmTempExperts")
	@ResponseBody
	public ResultMsg confirmTempUsers(HttpServletRequest request,HttpServletResponse response,String expertListString) throws Exception{
		try{
			expertService.confirmTempExperts(expertListString);
	        return new ResultMsg(ResultMsg.SUCCESS,"确认成功");
		}catch(Exception e){
			e.printStackTrace();
			return new ResultMsg(ResultMsg.FAIL,e.getMessage());
		}
	}
	
	@RequestMapping("/downloadTemp")
	public void downloadTemp(HttpServletRequest request,HttpServletResponse response) throws Exception{
		 String path = PropUtil.get("download.url")+"ExpertTemp.xlsx";
		 File file = new File(path);
         // 取得文件名。
         String filename = file.getName();
         // 以流的形式下载文件。
         InputStream fis = new BufferedInputStream(new FileInputStream(path));
         byte[] buffer = new byte[fis.available()];
         fis.read(buffer);
         fis.close();
         // 清空response
         response.reset();
         // 设置response的Header
         response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
         response.addHeader("Content-Length", "" + file.length());
         OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
         response.setContentType("application/octet-stream");
         toClient.write(buffer);
         toClient.flush();
         toClient.close();
	}

}
